﻿using System;
using UnityEngine;

namespace WentStateMachine
{
    [Serializable]
    public struct Condition
    {
        private string _parameter;
        //The name of the parameter used in the condition.
        public string parameter {
            get
            {
                return _parameter;
            }
            set {
                parameterHash = Helper.ScriptingStringToCRC32(value);
                _parameter = value;
            }
        }

        [SerializeField] private ConditionMode _mode;
        //The mode of the condition.
        public ConditionMode mode
        {
            get
            {
                return _mode;
            }
            set
            {
                _mode = value;
            }
        }

        [SerializeField] private int _parameterHash;
        public int parameterHash
        {
            get
            {
                return _parameterHash;
            }
            set
            {
                _parameterHash = value;
            }
        }

        [SerializeField] private float _threshold;
        //The AnimatorParameter's threshold value for the condition to be true.
        public float threshold {
            get {
                return _threshold;
            }
            set {
                _threshold = value;
            }
        }

        public override bool Equals(object obj)
        {
            Condition other = (Condition)obj;
            FloatToInt convert1 = new FloatToInt
            {
                target = threshold
            };
            FloatToInt convert2 = new FloatToInt
            {
                target = other.threshold
            };
            return mode == other.mode && parameterHash == other.parameterHash && convert1.result == convert2.result;
        }

        public override int GetHashCode()
        {
            int prime = 31;
            int result = 1;
            FloatToInt convert = new FloatToInt
            {
                target = threshold
            };
            result = prime * result + (mode.GetHashCode());
            result = prime * result + convert.result;
            result = prime * result + parameterHash;
            return result;
        }
    }
}
